Pass features to native build commands
authorAlex Crichton <alex@alexcrichton.com>
Thu, 16 Oct 2014 17:33:35 +0000 (10:33 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 16 Oct 2014 17:33:35 +0000 (10:33 -0700)
Closes #97
Closes #601 (this is an equivalent solution for that problem)

src/cargo/ops/cargo_rustc/mod.rs
src/doc/native-build.md
tests/test_cargo_compile.rs

index c035441950d60516517203115247209263e1d247..45d51b9dc586e9b48b46401e3e8acc0b9383fae2 100644 (file)
@@ -216,6 +216,20 @@ fn compile_custom(pkg: &Package, cmd: &str,
     for arg in cmd {
         p = p.arg(arg);
     }
+    match cx.resolve.features(pkg.get_package_id()) {
+        Some(features) => {
+            for feat in features.iter() {
+                let feat = feat.as_slice().chars()
+                               .map(|c| c.to_uppercase())
+                               .map(|c| if c == '-' {'_'} else {c})
+                               .collect::<String>();
+                p = p.env(format!("CARGO_FEATURE_{}", feat).as_slice(), Some("1"));
+            }
+        }
+        None => {}
+    }
+
+
     for &(pkg, _) in cx.dep_targets(pkg).iter() {
         let name: String = pkg.get_name().chars().map(|c| {
             match c {
index b06f9679369ebf641f6ca32f289fa8f280511595..da6dcf56457046177e74c47161d25cdf4a63623d 100644 (file)
@@ -84,6 +84,10 @@ commands.
                          profile currently being built.
 * `PROFILE` - name of the profile currently being built (see
               [profiles][profile]).
+* `CARGO_FEATURE_<name>` - For each activated feature of the package being
+                           built, this environment variable will be present
+                           where `<name>` is the name of the feature uppercased
+                           and having `-` translated to `_`.
 
 [profile]: manifest.html#the-[profile.*]-sections
 
index b0ff9d7165d60b025200cf424f37199d425c1c5b..21caae85202343b86a439fb71df2d9cae13bca8e 100644 (file)
@@ -745,6 +745,9 @@ test!(custom_build_env_vars {
             version = "0.5.0"
             authors = ["wycats@example.com"]
 
+            [features]
+            foo = []
+
             [[bin]]
             name = "foo"
         "#)
@@ -753,6 +756,7 @@ test!(custom_build_env_vars {
             use std::io::fs::PathExtensions;
             fn main() {{
                 let _ncpus = os::getenv("NUM_JOBS").unwrap();
+                let _feat = os::getenv("CARGO_FEATURE_FOO").unwrap();
                 let debug = os::getenv("DEBUG").unwrap();
                 assert_eq!(debug.as_slice(), "true");
 
@@ -777,7 +781,8 @@ test!(custom_build_env_vars {
             }}
         "#,
         p.root().join("target").join("native").display()));
-    assert_that(build.cargo_process("build"), execs().with_status(0));
+    assert_that(build.cargo_process("build").arg("--features").arg("foo"),
+                execs().with_status(0));
 
 
     p = p
@@ -789,6 +794,9 @@ test!(custom_build_env_vars {
             authors = ["wycats@example.com"]
             build = '{}'
 
+            [features]
+            foo = []
+
             [[bin]]
             name = "foo"
 
@@ -798,7 +806,8 @@ test!(custom_build_env_vars {
         .file("src/foo.rs", r#"
             fn main() {}
         "#);
-    assert_that(p.cargo_process("build"), execs().with_status(0));
+    assert_that(p.cargo_process("build").arg("--features").arg("foo"),
+                execs().with_status(0));
 })
 
 test!(crate_version_env_vars {